package com.zk.shop.mapper;

import com.zk.shop.pojo.Manager;
import com.zk.shop.pojo.ManagerMenu;
import com.zk.shop.pojo.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ManagerMapper {
    //管理员登录
    @Select("select mid,mno,loginarea,loginip,lastlogintime,mpwd from managetable where mno=#{mno} and mpwd=#{mpwd}")
    Manager login(Manager manager);
    //新增管理员
    @Insert("insert into managetable(mno,mpwd,login_success) values(#{mno},#{mpwd},#{login_success})")
    Integer pushManager(Manager manager);
    //修改密码
    @Update("update managetable set mpwd=#{mpwd} where mid=#{mid}")
    Integer setPwd(Manager manager);
    //删除管理员
    @Delete("delete from managetable where mid=#{mid}")
    Integer removeManager(Integer mid);
    //更新登陆状态
    @Update("update managetable set login_success=#{login_success},loginip=#{loginip},loginarea=#{loginarea},lastlogintime=#{lastlogintime} where mid=#{mid}")
    Integer saveLoginStatus(Manager manager);
    //2分钟不操作页面更新登陆状态，退出登录
    @Update("update managetable set login_success=#{login_success} where mid=#{mid}")
    Integer updateLoginStatus(Manager manager);
    //重置账户信息
    @Update("update managetable set mpwd=#{mpwd},login_success=#{login_success} where mid=#{mid}")
    Integer savePassword(Manager manager);
    //设置级别
    @Update("update managetable set mrole=#{mrole} where mid=#{mid}")
    Integer saveRole(Integer mrole,String mid);
    @Update("<script>update managetable set mrole=#{mrole} where mid in <foreach collection ='arrs' item ='items' index ='index' separator=',' open='(' close=')'  > "+"#{items.mid}"+"</foreach> </script>")
    Integer saveListRole(Integer mrole, List<Manager> arrs);
    //管理员账户分页查询
    @Select("select mid,mno,mrole,login_success,loginarea,loginip,lastlogintime from managetable where mno like #{mno} and mrole = #{mrole} and login_success=#{login_success}")
    List<Manager> manageTableList(Manager manager);
    //获取管理员登陆状态
    @Select("select login_success from managetable where mid=#{mid}")
    Integer getLoginStatusI(Integer mid);

    @Select("select menuid,m.lastmenuid as lastmid,m.menuname as menuname,m.htmlpath as htmlpath,rm.roleid as roleid from rolemenu rm INNER JOIN menu m where rm.menuid = m.id and rm.lastmenuid is NULL and rm.roleid in (select roleid from rolemanager where managerid = #{managerid});")
    @Results({
            @Result(property = "menuname",column = "menuname"),
            @Result(property = "mm",column = "{menuid=menuid,roleId=roleid}", many = @Many(select = "com.zk.shop.mapper.ManagerMapper.getMenuTreeDown"))
    })
    List<ManagerMenu> getMenuTree(Integer managerid);

    @Select("select distinct * from menu where find_in_set(id,getMenuChildList(#{menuid})) and id in (select menuid from rolemenu where roleid = #{roleId})")
    List<Menu> getMenuTreeDown(Integer menuid,Integer roleId);
}
